//Source file: I:\\ISS\\UNIT3\\JAVA PACKAGE\\edu\\nus\\iss\\t5\\jass\\controller\\JobAdvertisementController.java

//REVISION HISTORY
//ARUN 17/10/2010 MODIFYING CREATE
//Ei Han 17/10/2010 MODIFYING DELETE

    /**
     * @author Arunkumar : Added functionalities for the AddJobAdvertisment
     * @author EiHan : Added functionalities for the Delete JobAdvertisment
     * @author Noel : Added functionalities for the Update JobAdvertisment
     * 
     */

package edu.nus.iss.t5.jass.controller;

import java.awt.Button;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import edu.nus.iss.t5.jass.entity.Company;
import edu.nus.iss.t5.jass.entity.Entity;
import edu.nus.iss.t5.jass.entity.JobAdvertisement;
import edu.nus.iss.t5.jass.exception.DAOException;
import edu.nus.iss.t5.jass.dao.JobAdvertDAO;

/**
 * This is controll class for JobAdvertisement. It invokes DAO to get Entity class, and 
 * it is called by UI class to display the entity.
 */
public class JobAdvertisementController extends Button 
{
   public FormSearchAdvertisement searchScreen;
   public static int STATUS_DELETED = -1;
   public JobAdvertisement jobadvertisement;
     
   static JobAdvertisement job;
   static Company comp;
   
   JobAdvertDAO jdao=null;
   private static JobAdvertisementController jac = null;
   private static int counter=0;
   /**
    * @roseuid 4CB4692D0313
    */
   private JobAdvertisementController() 
   {
	   try {
		jdao=new JobAdvertDAO();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	   
	   
   }
   
   public void setJobAdvertisement(JobAdvertisement job)
   {
	   this.job =job;
   }
   
   public JobAdvertisement getJobAdvertisement()
   {
	   return job;
   }
   /**
    * Get JobAdvertisement Controller instance.
    * This class implements Singleton pattern to ensure that only one instance can be created.
    * @return Job Response Controller Instance
    */
   public static JobAdvertisementController getInstance (){

	       if(jac == null)
	       {
	           synchronized(JobAdvertisementController.class) {
	             if (jac == null)
	                jac = new JobAdvertisementController();
	            }
	       }
	       return jac;
   }
      
   
   /**
    * @roseuid 4C52897A033B
    */
   
   //job details
   
   
   //company details
   
   /**
    * Create new JobAdvertisement
    * This method accepts an JobAdvertisement as argument, which encapsulates all
    * information of a new JobAdvertisement, and creates a new object with company 
    * and location. Another public method is automatically called to save this new 
    * job advertisement.
    */
   public void createJobAdvertisement(JobAdvertisement jobdet) 
   {
	    job= new JobAdvertisement();
	    comp = new Company(); 
	    //String advID;
	    //advID = CreateJobAdvertisementID();
	    //job.setBaseCode(advID);
	    job.setTitle(jobdet.title);
	    job.setJobDescription(jobdet.getJobDescription());
	    job.setTechnicalSkills(jobdet.getTechnicalSkills());
	    job.setManagementSkills(jobdet.getManagementSkills());
	    job.setRequiredWorkingExperience(jobdet.getRequiredWorkingExperience());
	    job.setSalaryMin(jobdet.getSalaryMin());
	    job.setSalaryMax(jobdet.getSalaryMax());
	    job.setStatus(jobdet.getStatus());
	    job.setRequiredStartDate(jobdet.getRequiredStartDate());
	    job.setOwner(jobdet.getOwner());
	    job.setCreationDate(jobdet.getCreationDate());
	    job.setModifyDate(jobdet.getModifyDate());
	    job.setPostingDate(jobdet.getCreationDate());
	    job.setExpiryDate(jobdet.getExpiryDate());
	    //loc.continent = jobdet.country;
	    //loc.country = jobdet.country;
	    //comp.setName(jobdet.companyname);
	    this.submitJobAdvertisementDeatilsForcreation(job);
	    //return advID;
	    
   }
   
   /**
    * @roseuid 4C528AB60054
    */
   
   //calls the DAO for creation
   
   /**
    * This method calls DAO class to save the JobAdvertisement
    */
   public void submitJobAdvertisementDeatilsForcreation(JobAdvertisement job) 
   {
	   try {
		jdao = new JobAdvertDAO();
		jdao.save((Entity)job);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}catch(Exception exe){
		exe.printStackTrace();
	}
	   
   }
   
   
   
   /**
    * @roseuid 4C528BC402F2
    */
   public void confirmCreateAdvert() 
   {
    
   }
   
   /**
    * @roseuid 4C52908F028E
    */
   public void processWithdraw() 
   {
    
   }
   
   /**
    * @roseuid 4C9D82A303BD
    */
   public void ShowDetailForm() 
   {
    
   }
   
   /**
    * @roseuid 4C9D829A0014
    */
   public void RetrieveJobAdvert() 
   {
    
   }
   
   /**
    * @roseuid 4C52920703DE
    */
   public void withdrawAdvertisement() 
   {
    
   }
   
   /**
    * @roseuid 4C5293100197
    */
   public void confirmWithdrawAdverts() 
   {
    
   }
   
   /**
    * @roseuid 4C5295D903CF
    */
   public void updateAdvertisement() 
   {
	   
   }
   
   /**
    * @roseuid 4C5295FD01BC
    */
   public void processUpdateAdverts() 
   {
    
   }
   
   /**
    * @roseuid 4C9D8B420036
    */
   public void getResponse() 
   {
    
   }
   
   
   
   /**
    * Start Search Advertisement
    * This method receive FormSearchAdvertisement which encapsulates all search criteria
    * It invokes DAO class to retrieve all existing JobAdvertisement, and validates 
    * each Job Advertisement if it matches search criteria. 
    * @param FormSearchAdvertisement
    * @return List of JobAdvertisement
    */
   public  List <JobAdvertisement> StartSearchAdvertisement(FormSearchAdvertisement sjform) 
   {
	   List <JobAdvertisement> res = new ArrayList<JobAdvertisement>(); 
	   
	   Entity[] jobs = jdao.getAll();
	   //get all function will be provided by Azeem in DAO class
	   if (jobs == null){
		   return null;  //if jobs null, can't get the job list
	   }
	   for (int i=0;i<jobs.length;i++){
		   boolean match = false;
		   JobAdvertisement j=(JobAdvertisement)jobs[i];
		   if(j.getStatus()!=STATUS_DELETED){
			   System.out.println("j.getStatus() "+j.getStatus());
		   //System.out.println(sjform.description);
		   if (sjform.description != null && sjform.description.trim()!="" ){
			   StringTokenizer st = new StringTokenizer(sjform.description," ");
			   String s;
			   while (st.hasMoreTokens()){
				   s = st.nextToken();
				//   System.out.println(s+" : "+j.getJobDescription()+" : "+j.getJobDescription().toUpperCase().indexOf(s.toUpperCase()));				   
				   if(j.getJobDescription().toUpperCase().indexOf(s.toUpperCase())>=0){
					   match = true;
					   break;
				   }
			   }
		  }
		   //System.out.println(sjform.title);		   
		   if (sjform.title != null && sjform.title.trim()!="" && !match ){
			   StringTokenizer st = new StringTokenizer(sjform.title, " ");
			   String s;
			   while (st.hasMoreTokens()){
				   s = st.nextToken();
			//	   System.out.println(s+" | "+j.getTitle()+" | "+j.getTitle().toUpperCase().indexOf(s.toUpperCase()));
				   if(j.getTitle().toUpperCase().indexOf(s.toUpperCase())>=0){
					   match = true;
					   break;
				   }
			   }
		  }
		   if(match) {
			   res.add(j);
			   System.out.println("added :"+j.baseCode);
		   }
		   }
		  
		   
		/*   
		   if (sjform.last_n_date >= 0 ){
			   Calendar cal = Calendar.getInstance();
			   cal.setTimeInMillis(cal.getTimeInMillis()-sjform.last_n_date*24*3600*1000);
			   if (cal.after(j.getRequiredStartDate())){
				   res.add(j);
				   continue;
			   }
		   }
		   if (sjform.skill != null){
			   if (j.technicalSkills != null && j.technicalSkills.contains(sjform.skill.subSequence(0,sjform.skill.length()-1))){
				   res.add(j);
				   continue;
			   }
			   if (j.managementSkills != null && j.managementSkills.contains(sjform.skill.subSequence(0,sjform.skill.length()-1))){
				   res.add(j);
				   continue;
			   }
		   }
		   if (sjform.location != null ){
			       // no location link to ad   
		   }
		   if (sjform.lower_limmit >= 0 || sjform.upper_limmit >= sjform.lower_limmit){
			   if (j.getSalaryMin() >= sjform.lower_limmit && j.getSalaryMin() < sjform.upper_limmit){
				   res.add(j);
				   continue;
			   }
			   if (j.getSalaryMax() < sjform.upper_limmit && j.getSalaryMax() >= sjform.lower_limmit){
				   res.add(j);
				   continue;
			   }
		   }// salary search if
		   */
	   }//for loop
	   return res;
   }
      
   /**
    * Get JobAdvertisement
    * This method invokes DAO class to find the JobAdvertisement with
    * particular ID 
    * @return List of JobAdvertisement
    */
   
   public Entity getJobAdvertisement(String id) throws ParseException
   {
	   return jdao.get(id); 
   }
   
   /**
    * Update JobAdvertisement
    * This method accepts an JobAdvertisement as argument, which encapsulates all
    * information to update JobAdvertisement.
    * @param JobAdvertisement
    */
   
   public void updateJobAdvertisement(JobAdvertisement job)
   {
	   try {
			jdao = new JobAdvertDAO();
			jdao.update((Entity)job);
			Entity e = (Entity)job;
			System.out.println("ID in update"+e.getId() + "key "+e.getKey());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(Exception exe){
			exe.printStackTrace();
		}
   }
   
   /**
    * @roseuid 4C5296BD037E
    */
   public void validate() 
   {
    
   }
   
   /**
    * @roseuid 4C52972101D7
    */
   public void confirmUpdatedChanges() 
   {
    
   }
   
   /**
    * @roseuid 4C529A160091
    */
   public void deleteJobAvertisement() 
   {
    
   }
   
   /**
    * Method for deleting job advertisement
    * @param JobAdvertisement 
    */
   public void confirmDeleteJobAdvertisement(JobAdvertisement jad) throws DAOException 
   {
	  
	   try {
		jdao.remove(jad);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
   }
   
   /**
    * @roseuid 4C9D8B1B00C3
    */
   public void getAllResponses() 
   {
    
   }
   
   /**
    * @roseuid 4CA94B33002D
    */
   public void opname() 
   {
    
   }
   
   /**
    * @roseuid 4CAF44D80274
    */
   public void Reset() 
   {
    
   }
}
